﻿using Mogubang_Sl.Core;
using Mogubang_Sl.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mogubang_Sl.Utils
{
    public class MoguLogicHelper
    {
        static Random r = new Random(DateTime.Now.Millisecond);

        public static bool IsCanContinueHebin(MoguType type, int moguLevel)
        {
            bool result = false;

            //蓝色Mogu可以合并到8级
            //橘色合并到4级
            //ET合并到3级
            //爱心只能1级
            if (type == MoguType.BlueMogu && moguLevel < 11)
            {
                result = true;
            }
            else if (type == MoguType.OrangeMogu && moguLevel < 4)
            //else if (type == MoguType.OrangeMogu && moguLevel < 0)
            {
                result = true;
            }
            else if (type == MoguType.ET && moguLevel < 2)
            {
                result = true;
            }

            return result;
        }

        /// <summary>
        /// Mogu添加逻辑
        /// </summary>
        /// <param name="isGenET"></param>
        /// <returns></returns>
        public static MoguType GenMoguType(bool isGenET)
        {
            MoguType result = MoguType.Null;

            int sand = r.Next(200);
            if (isGenET)
            {
                if (sand <= 70)
                {
                    result = MoguType.BlueMogu;
                }
                else if (sand <= 120)
                {
                    result = MoguType.OrangeMogu;
                }
                else if (sand <= 170)
                {
                    result = MoguType.NewLife;
                }
                else
                {
                    result = MoguType.ET;
                }
            }
            else
            {
                if (sand <= 80)
                {
                    result = MoguType.BlueMogu;
                }
                else if (sand <= 140)
                {
                    result = MoguType.OrangeMogu;
                }
                else
                {
                    result = MoguType.NewLife;
                }
            }
            return result;
        }

        public static int GenLevel(MoguType type)
        {
            int level = 1;

            if (type == MoguType.BlueMogu)
            {
                int sand = r.Next(220);
                if (sand < 120)
                {
                    level = 1;
                }
                else if (sand < 180)
                {
                    level = 2;
                }
                else if (sand < 200)
                {
                    level = 3;
                }
                else
                {
                    level = 4;
                }
            }
            else if (type == MoguType.NewLife)
            {
                level = 1;
            }
            else if (type == MoguType.ET)
            {
                level = 1;
            }
            else if (type == MoguType.OrangeMogu)
            {
                int sand = r.Next(200);
                if (sand < 120)
                {
                    level = 1;
                }
                else if (sand < 180)
                {
                    level = 2;
                }
                else
                {
                    level = 3;
                }
            }

            return level;
        }

        /// <summary>
        /// 检查移动的方向能否被移动，主要检测是否被锁定
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="direction">0-Left 1-Right 2-Top 3-Bottom</param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        public static bool CanMove(MoguData[,] datas, int direction, int selectRow, int selectColumn, int emptyRow, int emptyColumn)
        {
            bool result = true;

            if (direction == 0)
            {
                //向左移动
                //判断从空的到移动选择点中间是否有被锁定的item
                //判断其上下左右的节点中是否有罩着自己的et
                for (int i = emptyColumn + 1; i <= selectColumn; i++)
                {
                    if (IsLocked(selectRow, i, datas))
                    {
                        result = false;
                        break;
                    }
                }
            }
            else if (direction == 1)
            {
                //向右移动
                for (int i = selectColumn; i < emptyColumn; i++)
                {
                    if (IsLocked(selectRow, i, datas))
                    {
                        result = false;
                        break;
                    }
                }
            }
            else if (direction == 2)
            {
                //向上移动
                for (int i = emptyRow + 1; i <= selectRow; i++)
                {
                    if (IsLocked(i, selectColumn, datas))
                    {
                        result = false;
                        break;
                    }
                }
            }
            else if (direction == 3)
            {
                //向右移动
                for (int i = selectRow; i < emptyRow; i++)
                {
                    if (IsLocked(i, selectColumn, datas))
                    {
                        result = false;
                        break;
                    }
                }
            }

            return result;
        }

        private static bool IsLocked(int r, int c, MoguData[,] datas)
        {
            bool result = false;

            if (datas[r, c].Type == MoguType.ET)
            {
                return false;
            }

            //上
            if (r > 0)
            {
                MoguData temp = datas[r - 1, c];
                if (temp != null)
                {
                    if (temp.Type == MoguType.ET
                        && temp.Level == 1
                        && temp.ET_Side == 3)
                    {
                        result = true;
                    }
                }
            }
            //下
            if (r < GameStatics.RowColumnCount - 1)
            {
                MoguData temp = datas[r + 1, c];
                if (temp != null)
                {
                    if (temp.Type == MoguType.ET
                        && temp.Level == 1
                        && temp.ET_Side == 2)
                    {
                        result = true;
                    }
                }
            }
            //左
            if (c > 0)
            {
                MoguData temp = datas[r, c - 1];
                if (temp != null)
                {
                    if (temp.Type == MoguType.ET
                        && temp.Level == 1
                        && temp.ET_Side == 1)
                    {
                        result = true;
                    }
                }
            }
            //右
            if (c < GameStatics.RowColumnCount - 1)
            {
                MoguData temp = datas[r, c + 1];
                if (temp != null)
                {
                    if (temp.Type == MoguType.ET
                        && temp.Level == 1
                        && temp.ET_Side == 0)
                    {
                        result = true;
                    }
                }
            }

            return result;
        }

        public static int GetCurrentET1Count(MoguData[,] datas)
        {
            int count = 0;

            for (int i = 0; i < GameStatics.RowColumnCount; i++)
            {
                for (int j = 0; j < GameStatics.RowColumnCount; j++)
                {
                    if (datas[i, j] != null && datas[i, j].Type == MoguType.ET && datas[i, j].Level == 1)
                    {
                        count++;
                    }
                }
            }

            return count;
        }
    }
}
